package com.yiren.manager.ctrl.elder;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.entity.YrElder;
import com.yiren.entity.YrElderBedRecord;
import com.yiren.entity.YrStaffInfo;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.base.CommParamForm;
import com.yiren.manager.form.elder.YrElderBedRecordForm;
import com.yiren.manager.form.elder.YrElderForm;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.service.iface.elder.YrElderBedRecordService;
import com.yiren.manager.service.iface.elder.YrElderService;
import com.yiren.manager.utils.ObjectUtils;

@Controller
@RequestMapping("elderBedRecord")
public class YrElderBedRecordController extends GenericController<YrElderBedRecordForm, YrElderBedRecord>{

	@Autowired
	private YrElderBedRecordService selfService;
	
	@Autowired
	private YrElderService elderService;
	
	@Autowired
	private YrStaffInfoService staffInfoService;
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrElderBedRecordForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrElderBedRecordForm> result = new HTTPResult<YrElderBedRecordForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrElderBedRecordForm form = tool.mapper("elderBedRecord", YrElderBedRecordForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getId()){
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		YrElderBedRecord entity = ObjectUtils.objectToObject(form, YrElderBedRecord.class);
		selfService.save(entity);
		
		return result;
	}
	
	@RequestMapping("test")
	@ResponseBody
	public HTTPResult<YrElderBedRecordForm> test(String commParamForm) throws SwallowException{
		HTTPResult<YrElderBedRecordForm> result = new HTTPResult<YrElderBedRecordForm>();
		
		JsonMapperTool tool = new JsonMapperTool(commParamForm);
		
		System.out.println("loginedUser="+commParamForm);
		
		CommParamForm userLevel = tool.mapper("loginedUser",CommParamForm.class);
		
		
		System.out.println("userLevel="+userLevel.getUserLevel());
		return result;
	}
	
	@RequestMapping("findBySubjectRoomId")
	@ResponseBody
	public HTTPResult<YrElderBedRecordForm> findBySubjectRoomId(String commParamForm,String subjectRoomId) throws Exception{
		HTTPResult<YrElderBedRecordForm> result = new HTTPResult<YrElderBedRecordForm>();
		
		JsonMapperTool tool = new JsonMapperTool(commParamForm);
		CommParamForm loginedUser = tool.mapper("loginedUser",CommParamForm.class);
		
		String sqlWhere = null;
		QueryResult<YrElderBedRecord> qr  = null;
		if(loginedUser.getUserLevel()==1){//如果是超级管理员
			sqlWhere = "handle_status=0";
			qr = selfService.find(YrElderBedRecord.class, sqlWhere, new Object[]{});
		}else{
			sqlWhere = "subject_room_id=?1 AND handle_status=0";
			qr = selfService.find(YrElderBedRecord.class, sqlWhere, new Object[]{subjectRoomId});
		}
		
		List<YrElderBedRecordForm> formList = new ArrayList<YrElderBedRecordForm>();
		for(YrElderBedRecord entity : qr.getResultList()){
			YrElderBedRecordForm form = toForm(entity);
			YrElder elder = elderService.findOne(form.getElderId());
			if(null!=elder){
				form.setElderForm(ObjectUtils.objectToObject(elder, YrElderForm.class));
			}
			form.setElderName(elder==null?"":elder.getName());
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	@RequestMapping("handleBedRecord")
	@ResponseBody
	public HTTPResult<YrElderBedRecordForm> finishBatch(String data) throws MyException, SwallowException{
		HTTPResult<YrElderBedRecordForm> result = new HTTPResult<YrElderBedRecordForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		Integer[] ids = tool.mapper("ids", Integer[].class);
		if(null==ids){
			throwExp("param.error");
		}
		for(Integer id : ids){
			YrElderBedRecord entity = selfService.findOne(id);
			entity.setHandleStatus(1);
			selfService.save(entity);
		}
		return result;
	}
	
	@RequestMapping("findByElderId")
	@ResponseBody
	public HTTPResult<YrElderBedRecordForm> findByElderId(String elderId) throws Exception{
		HTTPResult<YrElderBedRecordForm> result = new HTTPResult<YrElderBedRecordForm>();
		if(elderId==null){
			throwExp("老者不存在");
		}
		YrElder elder = elderService.findOne(elderId);
		YrElderForm elderForm = ObjectUtils.objectToObject(elder, YrElderForm.class);
		String sqlWhere = "elder_id=?1";
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("createTime", "ASC");
		QueryResult<YrElderBedRecord> qr = selfService.find(YrElderBedRecord.class, sqlWhere, new Object[]{elderId},map);
		
		List<YrElderBedRecordForm> formList = new ArrayList<YrElderBedRecordForm>();
		
		for(YrElderBedRecord entity : qr.getResultList()){
			YrElderBedRecordForm elderBedRecordForm = toForm(entity);
			YrStaffInfo staffInfo = staffInfoService.findOne(elderBedRecordForm.getCreateId()==null?"":elderBedRecordForm.getCreateId());
			elderBedRecordForm.setHandler(staffInfo.getName());
			elderBedRecordForm.setElderForm(elderForm);
			formList.add(elderBedRecordForm);
		}
		
		result.setData(formList);
		return result;
	}
	
	@Override
	public YrElderBedRecordForm toForm(YrElderBedRecord e) throws Exception {
		YrElderBedRecordForm form = ObjectUtils.objectToObject(e, YrElderBedRecordForm.class);
		return form;
	}

	@Override
	public YrElderBedRecord toEntity(YrElderBedRecordForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
